
package com.compuvest.admin.update.v1

import groovy.sql.Sql
import javax.naming.InitialContext
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class SQLService {

    def log = LoggerFactory.getLogger(SQLService.class);
    def sql

    def SQLService() {

        def ds = new InitialContext().lookup("jdbc/mysql")
        sql = new Sql(ds)

        sql.eachRow('select VERSION()') { row ->
            log.info ">>>>UploadItems SQLService() version=${row[0]}"
        }
        sql.close()

        sql.execute("SET GLOBAL thread_cache_size=8")
        sql.close()
    }

// <editor-fold defaultstate="collapsed" desc="getPriceItems()">
/*
SELECT it.item_id,it.accwareCatID,it.vendor_id,iv.alias
FROM sklad.items it
LEFT JOIN sklad.itemVendor iv USING(vendor_id)
WHERE it.active='Y' AND it.accwareCatID IN ('311003739-02','311003740-02','503000291');
*/
    def getPriceItems(accwareids) {

        def items = [:]
        sql.eachRow(
            "SELECT accwareCatID,cost_price,sell_price,hot_price,stock_quantity,stock,active " +
            "FROM sklad.items " +
            "WHERE accwareCatID IN (${accwareids})")
        { row ->

            items[row.accwareCatID] = new Item(
                field: [
                    costprice:  row.cost_price,
                    sellprice:  row.sell_price,
                    reslprice:  row.hot_price,
                    quantity:   row.stock_quantity,
                    stock:      row.stock,
                    active:     row.active
                ]
            )
        }
        sql.close()

        return items
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="doBatchPrice()">

    def static final UPDATE_ITEM_PRICE =
	"UPDATE LOW_PRIORITY sklad.items it,sklad.itemsOnGoogle ig " +
        "SET ig.updated=NOW(),ig.actionToGoogle='update',it.cost_price=?,it.sell_price=?,it.hot_price=?,it.stock_quantity=?,it.stock=?,it.active=? " +
	"WHERE it.accwareCatID=? AND it.item_id=ig.item_id"

    def doBatchPrice(items) {

        sql.withBatch(items.size(), UPDATE_ITEM_PRICE) { stmt ->
            items.each { accwareid, item ->
                if(item.getResult() == 'Ok' && item.utils['action'] == 'price') {

                    stmt.addBatch(
                        item.field['costprice'],
                        item.field['sellprice'],
                        item.field['reslprice'],
                        item.field['quantity'],
                        item.field['stock'],
                        item.field['active'],
                        accwareid
                    )
                } // if
            }
        }
        sql.close()
    }
// </editor-fold>


// <editor-fold defaultstate="collapsed" desc="getDescItems()">
/*
SELECT it.accwareCatID accwareid,it.cost_price costprice,it.sell_price sellprice,it.hot_price reslprice,
it.stock_quantity qty,it.stock stock,
gc.accwareCatID parentid,gc.name parentname,sc.category_id catid,sc.accwareCatID categoryid,sc.name catname,
sc.searchName1 prefname1,sc.searchName2 prefname2,sc.searchName3 prefname3,sc.searchName4 prefname4,sc.searchName5 prefname5,
iv.vendor_id vendid,iv.alias alias,iv.name vendname,
it.name itemname,it.mfr_name mfrname,it.mfr_number mfrnumber,ia.mfrName advmfrname,ia.mfrNumber advmfrnumber,it.adv_price advprice,
it.mfr_link mfrlink,ia.keywords keywords,it.package_include packinclude,it.comments packcomment,
CAST(it.stockDate AS CHAR) stockdate,
it.weight weight,it.res_price1 reslprice1,it.res_price2 reslprice2,it.res_price3 reslprice3,
it.res_quantity2 reslqty2,it.res_quantity3 reslqty3,it.drone drone,
it.warrantyMask wtymask,it.warrantyPrice1 wtyprice1,it.warrantyPrice2 wtyprice2,it.warrantyPrice3 wtyprice3,im.lot lot,it.active active,
IFNULL(idc.xml,'') xml,IFNULL(idc.xmlsize,0) xmlsize,
pr.pref1 pref1,pr.pref2 pref2,pr.pref3 pref3,pr.pref4 pref4,pr.pref5 pref5,
ia.cnet cnet,ia.grabber grabber,ia.bizrate bizrate,ia.nexttag nextag,ia.shopping shopping,ia.powersource powersource,ia.froogle froogle,
im.special special,im.closeout closeout,im.commingsoon comingsoon,im.bestprice bestprice,im.firstpage firstpage,
im.categorypage categorypage,im.bannerpage ebay,ia.cj cj,ia.banner_search gadgetpage,ia.upc upccode
FROM sklad.items it
LEFT JOIN sklad.itemsDesc idc USING(item_id)
LEFT JOIN sklad.itemsAdvert ia ON ia.item_id=it.item_id
LEFT JOIN sklad.itemsMask im ON im.item_id=it.item_id
LEFT JOIN sklad.preference pr ON pr.item_id=it.item_id
LEFT JOIN sklad.itemVendor iv ON iv.vendor_id=it.vendor_id
LEFT JOIN sklad.category sc ON sc.category_id=it.category_id
LEFT JOIN sklad.category gc ON sc.parent=gc.category_id
WHERE it.accwareCatID IN ()

SELECT it.item_id,it.accwareCatID,it.vendor_id,iv.alias
FROM sklad.items it
LEFT JOIN sklad.itemVendor iv USING(vendor_id)
WHERE it.active='Y' AND it.accwareCatID IN ('311003739-02','311003740-02','503000291');
*/

    def getDescItems(accwareids) {

        def items = [:]
        sql.eachRow(
            "SELECT it.item_id itemid,it.accwareCatID accwareid,it.cost_price costprice,it.sell_price sellprice,it.hot_price reslprice," +
            "it.stock_quantity qty,it.stock stock," +
            "gc.accwareCatID parentid,gc.name parentname,sc.category_id catid,sc.accwareCatID categoryid,sc.name catname," +
            "sc.searchName1 prefname1,sc.searchName2 prefname2,sc.searchName3 prefname3,sc.searchName4 prefname4,sc.searchName5 prefname5," +
            "iv.vendor_id vendid,iv.alias alias,iv.name vendname," +
            "it.name itemname,ig.itemname advname,it.mfr_name mfrname,it.mfr_number mfrnumber,ia.mfrName advmfrname,ia.mfrNumber advmfrnumber,it.adv_price advprice," +
            "it.mfr_link mfrlink,ia.keywords keywords,it.package_include packinclude,it.comments packcomment," +
            "CAST(it.stockDate AS CHAR) stockdate," +//IF(it.stockDate='0000-00-00','0000-00-00',it.stockDate)
            "it.weight weight,it.res_price1 reslprice1,it.res_price2 reslprice2,it.res_price3 reslprice3," +
            "it.res_quantity2 reslqty2,it.res_quantity3 reslqty3,it.drone drone," +
            "it.warrantyMask wtymask,it.warrantyPrice1 wtyprice1,it.warrantyPrice2 wtyprice2,it.warrantyPrice3 wtyprice3,im.lot lot,it.active active," +
            "ig.groups googlegroup," +
            "IFNULL(idc.xml,'') xml,IFNULL(idc.xmlsize,0) xmlsize," +
            "pr.pref1 pref1,pr.pref2 pref2,pr.pref3 pref3,pr.pref4 pref4,pr.pref5 pref5," +
            "ia.cnet cnet,ia.grabber grabber,ia.bizrate bizrate,ia.nexttag nextag,ia.shopping shopping,ia.powersource powersource,ia.froogle froogle," +
            "im.special special,im.closeout closeout,im.commingsoon comingsoon,im.bestprice bestprice,im.firstpage firstpage," +
            "im.categorypage categorypage,im.bannerpage ebay,ia.cj cj,ia.banner_search gadgetpage,ia.upc upccode " +
            "FROM sklad.items it " +
            "LEFT JOIN sklad.itemsDesc idc USING(item_id) " +
            "LEFT JOIN sklad.itemsOnGoogle ig USING(item_id) " +
            "LEFT JOIN sklad.itemsAdvert ia ON ia.item_id=it.item_id " +
            "LEFT JOIN sklad.itemsMask im ON im.item_id=it.item_id " +
            "LEFT JOIN sklad.preference pr ON pr.item_id=it.item_id " +
            "LEFT JOIN sklad.itemVendor iv ON iv.vendor_id=it.vendor_id " +
            "LEFT JOIN sklad.category sc ON sc.category_id=it.category_id " +
            "LEFT JOIN sklad.category gc ON sc.parent=gc.category_id " +
            "WHERE it.accwareCatID IN (${accwareids})"
        ) { row ->

            items[row.accwareid] = new Item(
                category: [
                    parentid:       row.parentid,
                    parentname:     row.parentname,
                    categoryid:     row.categoryid,
                    catname:        row.catname,
                    prefname1:      row.prefname1,
                    prefname2:      row.prefname2,
                    prefname3:      row.prefname3,
                    prefname4:      row.prefname4,
                    prefname5:      row.prefname5
                ],
                vendor: [
                    alias:          row.alias,
                    name:           row.vendname
                ],
                field: [
                    costprice:      row.costprice,
                    sellprice:      row.sellprice,
                    reslprice:      row.reslprice,
                    stock:          row.stock,
                    itemname:       row.itemname,
                    advname:        row.advname,
                    mfrname:        row.mfrname,
                    mfrnumber:      row.mfrnumber,
                    advprice:       row.advprice,
                    mfrlink:        row.mfrlink,
                    packinclude:    row.packinclude,
                    packcomment:    row.packcomment,
                    stockdate:      row.stockdate,
                    weight:         row.weight,
                    reslprice1:     row.reslprice1,
                    reslprice2:     row.reslprice2,
                    reslprice3:     row.reslprice3,
                    reslqty2:       row.reslqty2,
                    reslqty3:       row.reslqty3,
                    drone:          row.drone,
                    wtymask:        row.wtymask,
                    wtyprice1:      row.wtyprice1,
                    wtyprice2:      row.wtyprice2,
                    wtyprice3:      row.wtyprice3,
                    googlegroup:    row.googlegroup,
                    active:         row.active
                ],
                quantity: [
                    qty:            row.qty
                ],
                fdesc: [
                    desc:           row.xml,
                    size:           row.xmlsize
                ],
                pref: [
                    pref1:          row.pref1,
                    pref2:          row.pref2,
                    pref3:          row.pref3,
                    pref4:          row.pref4,
                    pref5:          row.pref5
                ],
                advert: [
                    advmfrname:     row.advmfrname,
                    advmfrnumber:   row.advmfrnumber,
                    keywords:       row.keywords,
                    cnet:           row.cnet,
                    grabber:        row.grabber,
                    bizrate:        row.bizrate,
                    nextag:         row.nextag,
                    shopping:       row.shopping,
                    powersource:    row.powersource,
                    froogle:        row.froogle,
                    cj:             row.cj,
                    gadgetpage:     row.gadgetpage,
                    upccode:        row.upccode
                ],
                banner: [
                    special:        row.special,
                    closeout:       row.closeout,
                    comingsoon:     row.comingsoon,
                    bestprice:      row.bestprice,
                    firstpage:      row.firstpage,
                    categorypage:   row.categorypage,
                    ebay:           row.ebay,
                    lot:            row.lot
                ],
                utils: [
                    catid:          row.catid,
                    vendid:         row.vendid,
                    itemid:         row.itemid
                ],
            )
        }
        sql.close()

        return items
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="doBatchDesc()">

    def static final MODIFY_CATEGORY = "{ call sklad.modifyCategory(?,?,?,?,?,?,?,?,?,?,?) }" //parentid,parentname,categoryid,catname,prefName1,prefName2,prefName3,prefName4,prefName5,active,out_catid

    def static final MODIFY_VENDOR = "{ call sklad.modifyVendor(?,?,?) }" // alias,name,out_vendid

    def doBatchDesc(items) {

        Integer itemid = 0
        items.each { accwareid, item ->
            if(item.getResult() == 'Ok' && item.utils['action'] == 'desc') {
                println "newitem=" << item.getJSON()

                def catid = 0, vendid = 0

                if(item.needupdate['category']) {

                    sql.call(MODIFY_CATEGORY, [
                        item.category['parentid'],
                        item.category['parentname'],
                        item.category['categoryid'],
                        item.category['catname'],
                        item.category['prefname1'],
                        item.category['prefname2'],
                        item.category['prefname3'],
                        item.category['prefname4'],
                        item.category['prefname5'],
                        'Y',
                        Sql.INTEGER
                    ]) { id ->
                        catid = id
                    }
                    sql.close()
                }
                if(item.needupdate['vendor']) {

                    sql.call(MODIFY_VENDOR, [
                        item.vendor['alias'],
                        item.vendor['name'],
                        Sql.INTEGER
                    ]) { id ->
                        vendid = id
                    }
                    sql.close()
                }
                //println "update vendor: ${accwareid} : ${vendid}: item.vendoralias=${item.vendor['alias']}:${item.vendor['name']}"

                if("Update" == item.getResulttype()) {

                    sql.executeUpdate(
                        """UPDATE LOW_PRIORITY sklad.items SET
                        active='Y',reserv_quantity=0,etiliz_id=:etilid,category_id=:catid,
                        vendor_id=:vendid,name=:itemname,cost_price=:costprice,sell_price=:sellprice,hot_price=:reslprice,stock_quantity=:quantity,
                        mfr_name=:mfrname,mfr_number=:mfrnumber,adv_price=:advprice,mfr_link=:mfrlink,
                        stock=:stock,package_include=:packinclude,comments=:packcomment,stockDate=:stockdate,weight=:weight,active=:active,
                        res_price1=:reslprice1,res_price2=:reslprice2,res_price3=:reslprice3,res_quantity2=:reslqty2,res_quantity3=:reslqty3,
                        drone=:drone,warrantyMask=:wtymask,warrantyPrice1=:wtyprice1,warrantyPrice2=:wtyprice2,warrantyPrice3=:wtyprice3,keywords=:keywords
                        WHERE item_id=:itemid""",
                        [
                            etilid:         item.fdesc['etilid'],
                            catid:          catid  ? catid  : item.utils['catid'],
                            vendid:         vendid ? vendid : item.utils['vendid'],
                            itemname:       item.field['itemname'],
                            costprice:      item.field['costprice'],
                            sellprice:      item.field['sellprice'],
                            reslprice:      item.field['reslprice'],
                            quantity:       item.quantity['qty'],
                            stock:          item.field['stock'],
                            mfrname:        item.field['mfrname'],
                            mfrnumber:      item.field['mfrnumber'],
                            advprice:       item.field['advprice'],
                            mfrlink:        item.field['mfrlink'],
                            packinclude:    item.field['packinclude'],
                            packcomment:    item.field['packcomment'],
                            stockdate:      item.field['stockdate'],
                            weight:         item.field['weight'],
                            active:         item.field['active'],
                            reslprice1:     item.field['reslprice1'],
                            reslprice2:     item.field['reslprice2'],
                            reslprice3:     item.field['reslprice3'],
                            reslqty2:       item.field['reslqty2'],
                            reslqty3:       item.field['reslqty3'],
                            drone:          item.field['drone'],
                            wtymask:        item.field['wtymask'],
                            wtyprice1:      item.field['wtyprice1'],
                            wtyprice2:      item.field['wtyprice2'],
                            wtyprice3:      item.field['wtyprice3'],
                            keywords:       item.getKeywords(),
                            itemid:         item.utils['itemid']
                        ]
                    )
                    sql.close()

                    // desc
                    if(item.needupdate['desc']) {
                        sql.executeUpdate(
                            """UPDATE LOW_PRIORITY sklad.itemsDesc SET json=:json,xml=:xml,xmlsize=:xmlsize WHERE item_id=:itemid""",
                            [
                                json:       item.getJSON(),
                                xml:        item.fdesc['desc'],
                                xmlsize:    item.fdesc['desc'].length(),
                                itemid:     item.utils['itemid']
                            ]
                        )
                        sql.close()
                    }

                    // pref
                    if(item.needupdate['pref']) {
                        sql.executeUpdate(
                            """UPDATE LOW_PRIORITY sklad.preference SET pref1=:pref1,pref2=:pref2,pref3=:pref3,pref4=:pref4,pref5=:pref5 WHERE item_id=:itemid""",
                            [
                                pref1:  item.pref['pref1'],
                                pref2:  item.pref['pref2'],
                                pref3:  item.pref['pref3'],
                                pref4:  item.pref['pref4'],
                                pref5:  item.pref['pref5'],
                                itemid: item.utils['itemid']
                            ]
                        )
                        sql.close()
                    }

                    // advert
                    if(item.needupdate['advert']) {
                        sql.executeUpdate(
                            """UPDATE LOW_PRIORITY sklad.itemsAdvert SET
                            mfrName=:advmfrname,mfrNumber=:advmfrnumber,keywords=:keys,cnet=:cnet,grabber=:grabber,
                            bizrate=:bizrate,nexttag=:nexttag,shopping=:shopping,powersource=:powersource,
                            froogle=:froogle,cj=:cj,banner_search=:gadgetpage,upc=:upccode
                            WHERE item_id=:itemid""",
                            [
                                advmfrname:     item.advert['advmfrname'],
                                advmfrnumber:   item.advert['advmfrnumber'],
                                keys:           item.advert['keywords'],
                                cnet:           item.advert['cnet'],
                                grabber:        item.advert['grabber'],
                                bizrate:        item.advert['bizrate'],
                                nexttag:        item.advert['nextag'],
                                shopping:       item.advert['shopping'],
                                powersource:    item.advert['powersource'],
                                froogle:        item.advert['froogle'],
                                cj:             item.advert['cj'],
                                gadgetpage:     item.advert['gadgetpage'],
                                upccode:        item.advert['upccode'],
                                itemid:         item.utils['itemid']
                            ]
                        )
                        sql.close()
                    }
                    // banner
                    if(item.needupdate['banner']) {
                        sql.executeUpdate(
                            """UPDATE LOW_PRIORITY sklad.itemsMask SET
                            special=:special,closeout=:closeout,commingsoon=:commingsoon,bestprice=:bestprice,firstpage=:firstpage,
                            categorypage=:categorypage,bannerpage=:ebay,lot=:lot
                            WHERE item_id=:itemid""",
                            [
                                special:        item.banner['special'],
                                closeout:       item.banner['closeout'],
                                commingsoon:    item.banner['comingsoon'],
                                bestprice:      item.banner['bestprice'],
                                firstpage:      item.banner['firstpage'],
                                categorypage:   item.banner['categorypage'],
                                ebay:           item.banner['ebay'],
                                lot:            item.banner['lot'],
                                itemid:         item.utils['itemid']
                            ]
                        )
                        sql.close()
                    }

                    // if change only quantity - no need send to google
                    if(item.needupdate['field']) {
                        sql.executeUpdate(
                            """UPDATE LOW_PRIORITY sklad.itemsOnGoogle
                            SET updated=NOW(),actionToGoogle='update',itemname=?,groups=?,send=?
                            WHERE accwareCatID=? """, [
                                item.field['advname'],
                                item.field['googlegroup'],
                                item.advert['froogle'],
                                accwareid
                            ]
                        )
                        sql.close()
                    }

                }
                if("Create" == item.getResulttype()) {

                    itemid = sql.executeInsert(
                        """INSERT LOW_PRIORITY sklad.items
                        SET reserv_quantity=0,etiliz_id=:etilid,category_id=:catid,vendor_id=:vendid,accwareCatID=:accwareid,
                        name=:itemname,cost_price=:costprice,sell_price=:sellprice,hot_price=:reslprice,stock_quantity=:quantity,stock=:stock,
                        mfr_name=:mfrname,mfr_number=:mfrnumber,adv_price=:advprice,mfr_link=:mfrlink,package_include=:packinclude,comments=:packcomment,
                        stockDate=:stockdate,weight=:weight,active=:active,res_price1=:reslprice1,res_price2=:reslprice2,res_price3=:reslprice3,
                        res_quantity2=:reslqty2,res_quantity3=:reslqty3,drone=:drone,
                        warrantyMask=:wtymask,warrantyPrice1=:wtyprice1,warrantyPrice2=:wtyprice2,warrantyPrice3=:wtyprice3,
                        keywords=:keywords,createTime=NOW()""", [
                            etilid:         item.fdesc['etilid'],
                            catid:          catid,
                            vendid:         vendid,
                            accwareid:      accwareid,
                            itemname:       item.field['itemname'],
                            costprice:      item.field['costprice'],
                            sellprice:      item.field['sellprice'],
                            reslprice:      item.field['reslprice'],
                            quantity:       item.quantity['qty'],
                            stock:          item.field['stock'],
                            mfrname:        item.field['mfrname'],
                            mfrnumber:      item.field['mfrnumber'],
                            advprice:       item.field['advprice'],
                            mfrlink:        item.field['mfrlink'],
                            packinclude:    item.field['packinclude'],
                            packcomment:    item.field['packcomment'],
                            stockdate:      item.field['stockdate'],
                            weight:         item.field['weight'],
                            active:         item.field['active'],
                            reslprice1:     item.field['reslprice1'],
                            reslprice2:     item.field['reslprice2'],
                            reslprice3:     item.field['reslprice3'],
                            reslqty2:       item.field['reslqty2'],
                            reslqty3:       item.field['reslqty3'],
                            drone:          item.field['drone'],
                            wtymask:        item.field['wtymask'],
                            wtyprice1:      item.field['wtyprice1'],
                            wtyprice2:      item.field['wtyprice2'],
                            wtyprice3:      item.field['wtyprice3'],
                            keywords:       item.getKeywords()
                        ]
                    ).get(0).get(0) as Integer
                    sql.close()

                    sql.execute(
                        """INSERT LOW_PRIORITY sklad.itemsDesc SET item_id=:itemid,accwareCatID=:accwareid,json=:json,xml=:xml,xmlsize=:xmlsize""", [
                            itemid:     itemid,
                            accwareid:  accwareid,
                            json:       item.getJSON(),
                            xml:        item.fdesc['desc'],
                            xmlsize:    item.fdesc['desc'].length()
                        ]
                    )
                    sql.close()

                    // need delete from table mfrNameCnet,mfrNumberCnet,pricesaving,pricerunner
                    sql.execute(
                        """INSERT LOW_PRIORITY sklad.itemsAdvert SET item_id=:itemid,cnet=:cnet,grabber=:grabber,bizrate=:bizrate,nexttag=:nexttag,shopping=:shopping,
                        powersource=:powersource,froogle=:froogle,cj=:cj,banner_search=:gadgetpage,mfrName=:advmfrname,mfrNumber=:advmfrnumber,keywords=:keys,upc=:upccode""", [
                            itemid:         itemid,
                            cnet:           item.advert['cnet'],
                            grabber:        item.advert['grabber'],
                            bizrate:        item.advert['bizrate'],
                            nexttag:        item.advert['nextag'],
                            shopping:       item.advert['shopping'],
                            powersource:    item.advert['powersource'],
                            froogle:        item.advert['froogle'],
                            cj:             item.advert['cj'],
                            gadgetpage:     item.advert['gadgetpage'],
                            advmfrname:     item.advert['advmfrname'],
                            advmfrnumber:   item.advert['advmfrnumber'],
                            keys:           item.advert['keywords'],
                            upccode:        item.advert['upccode']
                        ]
                    )
                    sql.close()

                    sql.execute(
                        """INSERT LOW_PRIORITY sklad.itemsMask SET
                        item_id=:itemid,vendor_id=:vendid,special=:special,closeout=:closeout,commingsoon=:commingsoon,bestprice=:bestprice,
                        firstpage=:firstpage,categorypage=:categorypage,bannerpage=:ebay,lot=:lot""", [
                            itemid:         itemid,
                            vendid:         vendid,
                            special:        item.banner['special'],
                            closeout:       item.banner['closeout'],
                            commingsoon:    item.banner['comingsoon'],
                            bestprice:      item.banner['bestprice'],
                            firstpage:      item.banner['firstpage'],
                            categorypage:   item.banner['categorypage'],
                            ebay:           item.banner['ebay'],
                            lot:            item.banner['lot']
                        ]
                    ) //,page_b2b=?,page_hotdeals=?,page_closeout=?");
                    sql.close()

                    sql.execute(
                        """INSERT LOW_PRIORITY sklad.preference SET item_id=:itemid,pref1=:pref1,pref2=:pref2,pref3=:pref3,pref4=:pref4,pref5=:pref5""", [
                            itemid: itemid,
                            pref1:  item.pref['pref1'],
                            pref2:  item.pref['pref2'],
                            pref3:  item.pref['pref3'],
                            pref4:  item.pref['pref4'],
                            pref5:  item.pref['pref5']
                        ]
                    )
                    sql.close()

                    // items on google
                    sql.execute(
                        """INSERT LOW_PRIORITY sklad.itemsOnGoogle SET actionToGoogle='insert',updated=NOW(),item_id=?,accwareCatID=?,itemname=?,groups=?,send=? """, [
                            itemid,
                            accwareid,
                            item.field['advname'],
                            item.field['googlegroup'],
                            item.advert['froogle']
                        ]
                    )
                    sql.close()
                } // Create

            } // if
        }

    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getDeleteItems()">

    def getDeleteItems(accwareids) {

        def items = [:]
        sql.eachRow(
            "SELECT accwareCatID,active " +
            "FROM sklad.items " +
            "WHERE accwareCatID IN (${accwareids})"
        ) { row ->

            items[row.accwareCatID] = new Item(
                field: [
                    active: row.active
                ]
            )
        }
        sql.close()

        return items
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="doBatchDelete()">

    def static final DELETE_ITEM =
        "UPDATE LOW_PRIORITY sklad.items it,sklad.itemsOnGoogle ig " +
        "SET ig.updated=NOW(),ig.actionToGoogle='delete',it.active=? " +
	"WHERE it.accwareCatID=? AND it.item_id=ig.item_id"

    def doBatchDelete(items) {

        sql.withBatch(items.size(), DELETE_ITEM)
        { stmt ->
            items.each { accwareid, item ->
                if(item.getResult() == 'Ok' && item.utils['action'] == 'delete') {

                    stmt.addBatch('D', accwareid)
                }
            }
        }
        sql.close()

    }
// </editor-fold>


}

